package com.xuecheng.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CoursePubDTO;
import com.xuecheng.api.teaching.model.dto.CourseWorkRecDTO;
import com.xuecheng.api.teaching.model.qo.QueryWorkModel;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.teaching.agent.CourseBaseApiAgent;
import com.xuecheng.teaching.convert.CourseWorkRecConvert;
import com.xuecheng.teaching.entity.CourseWorkRec;
import com.xuecheng.teaching.mapper.CourseWorkRecMapper;
import com.xuecheng.teaching.service.CourseWorkRecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseWorkRecServiceImpl extends ServiceImpl<CourseWorkRecMapper, CourseWorkRec> implements CourseWorkRecService {

    @Autowired
    private CourseBaseApiAgent workApiAgent;

    @Override
    public PageVO<CourseWorkRecDTO> queryWork(PageRequestParams params, QueryWorkModel model, Long companyId) {
        // 1.判断业务数据
        // 1.1 判断分页数据
        if (params.getPageNo() < 1) {
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }
        if (params.getPageSize() < 1) {
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }

        LambdaQueryWrapper<CourseWorkRec> queryWrapper = new LambdaQueryWrapper<>();

        // 1.2 判断条件查询数据
        if (ObjectUtils.isEmpty(companyId) ||
                ObjectUtils.isEmpty(companyId)
        ) {
            throw new RuntimeException("公司ID不匹配");
        }
        String courseName = model.getCourseName();
        if (StringUtil.isNotBlank(courseName)) {
            RestResponse<CoursePubDTO> response = workApiAgent.getByCourseName4s(courseName);
            if (!response.isSuccessful()) {
                ExceptionCast.cast(CommonErrorCode.E_999981);
            }
            CoursePubDTO coursePubDTO = response.getResult();
            // 添加的课程名称条件
            queryWrapper.like(CourseWorkRec::getCoursePubId, coursePubDTO.getId());
        }

        // 2.创建分页数据
        Page<CourseWorkRec> page = new Page<>(params.getPageNo(), params.getPageSize());


        // 3.根据分页和查询调价查询list数据
        this.page(page, queryWrapper);


        List<CourseWorkRec> records = page.getRecords();

        long total = page.getTotal();
        List<CourseWorkRecDTO> results = CourseWorkRecConvert.INSTANCE.entitys2dtos(records);

        // 4.将查询结果封装到PageVO中

        PageVO pageVO = new PageVO(results, total, params.getPageNo(), params.getPageSize());

        return pageVO;

    }
}
